package top.lshaci.learning.alibaba.cloud.sentinel.gateway.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * GatewaySentinelConfig
 *
 * @author lshaci
 * @since 1.0.0
 */
@Configuration
public class GatewaySentinelConfig {

    /**
     * 配置请求被流控的处理器
     *  优先级低于配置文件中的 spring.cloud.sentinel.scg.fallback
     */
    @Bean
    public BlockRequestHandler blockRequestHandler() {
        return (exchange, t) -> {
            return ServerResponse
                    .ok()
                    .body(Mono.just("{\"message\":\"请求流量过大\",\"code\":50101}"), String.class);
        };
    }
}
